package com.xingxue.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xingxue.entity.Product;
import com.xingxue.entity.vo.*;
import com.xingxue.mapper.ProductMapper;

@RestController
public class ProductController {

    @Autowired
    public ProductMapper productMapper;

    @GetMapping("/product/datagrid")
    public DataGridVo dataGrid(@RequestParam(defaultValue="1") int page, @RequestParam(defaultValue="10") int rows,
        ProductVo vo) {
        List<Product> list = productMapper.findByVo(vo);
        int total = productMapper.findCountByVo(vo);
        return new DataGridVo(list, total);
    }

    @PostMapping("/product/save")
    public Map<String,Object> save(Product product){
        
        productMapper.save(product);
        Map<String,Object> map = new HashMap<>();
        map.put("success", true);
        return map;
    }

    @PostMapping("/product/update")
    public Map<String,Object> update(Product product){
        
        productMapper.update(product);
        Map<String,Object> map = new HashMap<>();
        map.put("success", true);
        return map;
    }

    @GetMapping("/product/findById")
    public Product findById(long id){
        return productMapper.findById(id);
    }

    @GetMapping("/product/deleteById")
    public Map<String,Object> delete(long id){
        productMapper.deleteById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("success", true);
        return map;
    }

    @GetMapping("/product/deleteByIds")
    public Map<String,Object> delete(@RequestParam List<java.lang.Long> id){
        productMapper.deleteByIds(id);
        Map<String,Object> map = new HashMap<>();
        map.put("success", true);
        return map;
    }
    

}